package com.googlecode.stockhamster.client.swing;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.company.ICompany;
import com.googlecode.stockhamster.stockmarket.IStockMarket;
import com.googlecode.stockhamster.stockstat.IStockStat;
import com.googlecode.stockhamster.utils.XFireStub;

public class CompanyPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9090409523649382525L;

	private static final String companyTableColumn[] = { "Company name",
			"Stock ID", "Stock Price", "Total assets", "Issue size",
			"Increasement rate", "Left in market" };

	private ICompany company;
	private IStockStat status;
	private IStockMarket market;

	private JTextField name;
	private JTextField category;
	private JTextField price;
	private JTextField amount;
	private JTextField rate;

	private JLabel companyResult;

	private JPanel operationPanel;
	private JScrollPane tablePanel;
	private JTable companyTable;

	public CompanyPanel() throws MalformedURLException {
		company = new XFireStub<ICompany>(Constant.companyUrl, ICompany.class)
				.getStub();
		status = new XFireStub<IStockStat>(Constant.stockStatUrl,
				IStockStat.class).getStub();
		market = new XFireStub<IStockMarket>(Constant.stockMarketUrl,
				IStockMarket.class).getStub();

		setLayout(new GridLayout(2, 1));
		operationPanel = new JPanel();
		operationPanel.add(new JLabel("Company name:"));
		name = new JTextField(8);
		operationPanel.add(name);
		operationPanel.add(new JLabel("Category:"));
		category = new JTextField(8);
		operationPanel.add(category);
		operationPanel.add(new JLabel("Price:"));
		price = new JTextField(5);
		operationPanel.add(price);
		operationPanel.add(new JLabel("Stock amount:"));
		amount = new JTextField(5);
		operationPanel.add(amount);
		operationPanel.add(new JLabel("Increasement rate:"));
		rate = new JTextField(3);
		operationPanel.add(rate);
		JButton add = new JButton("Add");
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (company.addCompany(Constant.companyAdminCode, name
						.getText(), category.getText(), Long.parseLong(price
						.getText()), Integer.parseInt(amount.getText()), Double
						.parseDouble(rate.getText())))
					companyResult.setText("Add: succeed");
				else
					companyResult.setText("Add: failed");
			}
		});
		operationPanel.add(add);
		JButton remove = new JButton("Remove");
		remove.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (company.deleteCompany(Constant.companyAdminCode, name
						.getText()))
					companyResult.setText("Remove: succeed");
				else
					companyResult.setText("Remove: failed");
			}
		});
		operationPanel.add(remove);
		JButton refresh = new JButton("Refresh");
		refresh.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String names[] = company
						.getCompanyList(Constant.companyAdminCode);
				Object contents[][] = new Object[names.length][7];
				for (int i = 0; i < names.length; i++) {
					Integer stockId = company.getStockID(names[i]);
					contents[i][0] = names[i];
					contents[i][1] = stockId;
					contents[i][2] = status.getPrice(stockId);
					contents[i][3] = company.getCapital(stockId);
					contents[i][4] = company.getIssueSize(stockId);
					contents[i][5] = company.getIncRate(
							Constant.companyAdminCode, names[i]);
					contents[i][6] = market.getStockAmountByAdmin(
							Constant.marketAdminCode, "market", stockId);
				}
				companyTable = new JTable(contents, companyTableColumn);
				tablePanel.setViewportView(companyTable);
			}
		});
		operationPanel.add(refresh);
		companyResult = new JLabel();
		operationPanel.add(companyResult);
		companyTable = new JTable(new Object[0][7], companyTableColumn);
		tablePanel = new JScrollPane(companyTable);
		add(operationPanel);
		add(tablePanel);
	}
}
